/**  
 * @Title ApayNotifyCtrl.java
 * @date 2016年4月26日 上午10:16:38
 * @Copyright (c) 2016, unibroad.com Inc. All rights reserved.
 */
package pay.ctrl;

import java.util.Map;

import base.ctrl.BaseController;
import base.jfinal.anatation.RouteBind;
import pay.biz.Dispatcher;
import pay.biz.PayTypeEnum;
import pay.util.ApayVerifyUtils;

/**
 * @Description 支付宝支付回调
 * @Class pay.ctrl.ApayNotifyCtrl
 * @author suan@unibroad.com
 * @version V1.0
 */
@RouteBind(path = "apay")
public class ApayInteractiveCtrl extends BaseController {

    /**
     * 支付宝相关调用回调接口
     * 
     * <pre>
     * {
            buyer_id=2088002646144423,
            total_amount=0.20,
            trade_no=2016040121001004420258235540,
            body=???0.20<U+052A>??,
            notify_time=2016-04-0117: 41: 41,
            open_id=20881013218043783991030251013042,
            subject=??????,
            sign_type=RSA,
            buyer_logon_id=sua***@163.com,
            notify_type=trade_status_sync,
            invoice_amount=0.20,
            out_trade_no=74430906816403456,
            gmt_payment=2016-04-0117: 41: 41,
            trade_status=TRADE_SUCCESS,
            point_amount=0.00,
            sign=IsGQL6kzuQJXaZaXEVpV2kQcwqF3lU3XRIr8zRXgriq0LlB5OREkcZGvHN/qvqwuIaoy1HQS2y72g4xQn2lCpguZzxppDOY9dSNLOO3jvchPbRfsbhDTlduupOPMLXBwgDcPRDT4b+oAzWbG7f/tUks6a4lpsq5P/sZ34+PhBjg=,
            gmt_create=2016-04-0117: 41: 13,
            buyer_pay_amount=0.20,
            receipt_amount=0.20,
            fund_bill_list=[
                {
                    "amount": "0.20",
                    "fundChannel": "ALIPAYACCOUNT"
                }
            ],
            app_id=2016032901250909,
            seller_id=2088611018786307,
            notify_id=2e397c63c9fbfbcc28fbfc629020b2ej8q,
            seller_email=eshop@touchus.com
        }
     * </pre>
     */
    public void notifymsg() {

        // 1. 获取参数,这里需要转码参数
        final Map<String, String> params = getRequestParams(true);
        log.debug(String.format("alipay notify req params %s", params.toString()));

        if (!ApayVerifyUtils.verifyNotify(params)) {
            log.error("notify req is Unreliable,Do not deal with it !");
            renderNull();
            return;
        }

        // 2.执行
        try {
            Dispatcher.getBizExecutor(PayTypeEnum.APAY_MOBILE, params).execute(params);
            renderJson("{\"result_code\":\"success\"}");
        } catch (Exception e) {
            log.error("alipay notify process exception", e);
        }
    }

}
